package com.tte.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @ClassName DateUtil
 * @Description
 * @Author DongWH
 * @Date 2024/11/4 14:19
 */
@Slf4j
public class DateUtil {

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM = "yyyy-MM";
    public static final String YYYYMM = "yyyyMM";
    public static final String YYYY = "yyyy";
    public static final String MM = "MM";
    public static final String DD = "dd";
    public static final String HH_MM_SS = "HH:mm:ss";
    public static final String HHMMSS = "HHmmss";
    public static final String HH_MM = "HH:mm";
    public static final String HHMM = "HHmm";

    public static final String[] DATE_FORMATS = {
            YYYY_MM_DD_HH_MM_SS, YYYYMMDDHHMMSS, YYYY_MM_DD, YYYYMMDD,
            YYYY_MM, YYYYMM, YYYY, MM, DD, HH_MM_SS, HHMMSS, HH_MM, HHMM
    };

    /**
     * 指定日期格式转换
     * @param dateStr 时间格式字符串
     * @param sourcePattern 源格式
     * @param targerPattern 目标格式
     * @return 时间格式字符串
     */
    public static String convertStr(String dateStr, String sourcePattern,String targerPattern) {
        try {
            DateTimeFormatter oldFormatter = DateTimeFormatter.ofPattern(sourcePattern);
            DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern(targerPattern);
            return LocalDateTime.parse(dateStr, oldFormatter).format(newFormatter);
        } catch (Exception e) {
            log.error("strToDate is Exception. e:", e);
            return null;
        }
    }

    public static Date convertToDate(String dateStr, String sourcePattern) {
        try {
            DateTimeFormatter oldFormatter = DateTimeFormatter.ofPattern(sourcePattern);
            return Date.from(LocalDateTime.parse(dateStr, oldFormatter).atZone(java.time.ZoneId.systemDefault()).toInstant());
        } catch (Exception e) {
            log.error("strToDate is Exception. e:", e);
            return null;
        }
    }

    public static String convertToStr(Date date, String targerPattern) {
        try {
            DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern(targerPattern);
            return LocalDateTime.ofInstant(date.toInstant(), java.time.ZoneId.systemDefault()).format(newFormatter);
        } catch (Exception e) {
            log.error("strToDate is Exception. e:", e);
            return null;
        }
    }

    public static String getFormat(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }

        for (String pattern : DATE_FORMATS) {
            try {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
                LocalDateTime.parse(str, formatter);
                return pattern;
            } catch (Exception e) {
                continue;
            }
        }
        return null;
    }
}